const express = require("express");
const cors = require("cors")
const db = require("./libs/DBHelper");
const { request } = require("express");

let server = express();

//支持post请求
server.use(express.urlencoded({ extended: false }));
server.use(express.json());
server.use(express.static("./../src"))

//拦截器 为了做跨域 CORS
server.use(cors());
// server.all("*", function (request, resposne, next) {
//     resposne.setHeader("content-Type", "application/json;charset=utf-8");
//     resposne.setHeader("Access-Control-Allow-Origin", "*");
//     next()
// })



//post 登录
server.post("/login", async function (request, resposne, next) {
    //1.接受前端发送过来的参数
    let oUser = request.body;
    //2.编写sql语句
    let sql = "SELECT `uId`,`uphone`,`upwd` FROM `users` WHERE uphone=? AND upwd=?;";
    //3.执行sql语句,带参数
    let res = await db.exec(sql, [oUser.phone, oUser.pwd]);
    //4.获取结果,进行判断
    let oMsg = {
        message: "登录失败",
        code: -200
    }
    if (res && res.length >= 1) {
        oMsg['message'] = "登陆成功";
        oMsg['code'] = "200";
        //当前登陆者的信息
        oMsg["data"] = res[0];
    }
    resposne.json(oMsg)

})

//post 注册
server.post("/reg", async function (request, resposne, next) {
    //1.接受前端发送过来的参数
    let oUser = request.body;
    //2.编写sql语句  "INSERT INTO  `users` (`uphone`,`upwd`) VALUES (?,?);"
    let sql = "SELECT `uId`,`uphone`FROM `users` WHERE uphone=?;";
    //3.执行sql语句,带参数
    let res = await db.exec(sql, [oUser.phone]);
    //4.获取结果,进行判断
    let oMsg = {
        message: "成功注册",
        code: 200
    }
    if (res && res.length >= 1) {
        oMsg['message'] = "注册已注册";
        oMsg['code'] = "-200";
        //当前登陆者的信息
        oMsg["data"] = res[0];
    }
    if (oMsg['code'] == 200) {
        let sql2 = "INSERT INTO  `users` (`uphone`,`upwd`) VALUES (?,?);";
        let res2 = db.exec(sql2, [oUser.phone, oUser.pwd]);
    }
    resposne.json(oMsg)
})

//加入购物车
server.post("/chart", async (request, response, next) => {
    let params = [
        request.body.pname,
        request.body.psize,
        request.body.pcolor,
    ]
    // console.log(params);
    let querySql = "SELECT * FROM `chart` WHERE pname=? AND psize=? AND `pcolor`=? AND `pstatus`=1;";
    let res = await db.exec(querySql, params);

    let status = (res && res.length >= 1);//有 -> true
    if (status) {
        //修改
        let paramsUpdate = [
            request.body.pnum,
            request.body.uphone,
            request.body.psize,
            request.body.pcolor,
        ]
        let updateSQL = 'UPDATE `chart` SET `pnum`=pnum+? ,pmoney= pnum*pprice WHERE uphone=? AND psize=? AND pcolor=? AND `pstatus`=1;'

        let res = await db.exec(updateSQL, paramsUpdate);
        //更新,删除,修改,返回受影响行数
        let status = (res && res.affectedRows >= 1);//修改成功

        response.json({
            message: status ? "加入购物车成功!u" : "加入购车失败u",
            code: 200,
            params
        })
    } else {
        //插入
        let params = [
            request.body.uphone,
            request.body.pname,
            request.body.psize,
            request.body.pcolor,
            request.body.pprice,
            request.body.pnum,
            request.body.pprice * request.body.pnum,
            request.body.pimg,
            request.body.shopname,
        ];
        console.log(params);
        let insertSQL = "INSERT INTO `chart` (`uphone`,`pname`,`psize`,`pcolor`,`pprice`,`pnum`,`pmoney`,`pimg`,`shopname`)VALUES(?,?,?,?,?,?,?,?,?);";
        let res = await db.exec(insertSQL, params);
        let status = (res && res.affectedRows >= 1); //插入成功 true

        response.json({
            message: status ? "加入购物车成功!i" : "加入购车失败i",
            code: 200,
            params
        })
    }
})

//查询购物车
server.post("/chartlist", async (request, response, next) => {

    let params = [
        request.body.uphone
    ]

    let sql = "SELECT * FROM `chart` WHERE `pstatus` =1 AND `uphone`=?;";
    let res = await db.exec(sql, params);

    response.json({
        message: "查询成功",
        data: res
    })
})

//修改商品数量
server.post("/chartNumber",async (request,response,next)=>{
    //修改
    let paramsUpdate = [
        request.body.pnum,
        request.body.uphone,
        request.body.psize,
        request.body.pcolor,
        request.body.pprice,

    ]
    let updateSQL = 'UPDATE `chart` SET `pnum`=? ,`pmoney`= pnum*pprice WHERE `uphone`=? AND `psize`=? AND `pcolor`=? AND `pprice`=? AND `pstatus`=1;'

    let res = await db.exec(updateSQL, paramsUpdate);
    //更新,删除,修改,返回受影响行数
    let status = (res && res.affectedRows >= 1);//修改成功

    response.json({
        message: status ? "加入购物车成功!u" : "加入购车失败u",
        code: 200,
        paramsUpdate,
        res
    })
})

//购物车商品删除
server.post("/chart/del",async (request,response,next)=>{
    //修改
    let paramsdel = [
        request.body.uphone,
        request.body.pname,
        request.body.psize,
        request.body.pcolor,
    ]
    let sql="UPDATE chart SET `pstatus`=0 WHERE `uphone`=? AND `pname`=? AND `psize`=? AND `pcolor`=?;";
    let res = await db.exec(sql, paramsdel);
    //因为修改,关注 受影响行数
    let status = (res && res.affectedRows >= 1);//true

    response.json({
        message: status ? "删除成功" : "删除失败",
        code: 200
    });
})

server.listen(8080, () => {
    console.log("服务器启动完毕!");
})